require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/iconfont.css");
require("./index.less");

// 引入swiper相关
require("../../libs/swiper-bundle.css");
const axios = require("../../utils/axios.js");
let Swiper = require("../../libs/swiper-bundle.js");
const dom = require("../../utils/dom");

document.addEventListener("DOMContentLoaded", function () {
	dom.renderFtr("index");
	var mySwiper = new Swiper(".swiper", {
		// direction: "vertical", // 垂直切换选项
		loop: true, // 循环模式选项

		autoplay: {
			delay: 3000,
			stopOnLastSlide: false,
			disableOnInteraction: false,
		},

		// 如果需要分页器
		pagination: {
			el: ".swiper-pagination",
		},
	});

	/* axios.defaults.baseURL = "http://fitness.h5.itsource.cn:3701";

	let token = localStorage.getItem("token");
	axios.defaults.headers.common["Authorization"] = "Bearer " + token; */
	// axios.defaults.headers.post["Content-Type"] = "/api/user/clockInInfo";

	// 获取用户个人信息
	axios.get("/api/user/info").then((res) => {
		// console.log(res);
		dom.get(".ranking span").textContent = res.data.data.ranking;
		dom.get(".badge span strong").textContent = res.data.data.badges;
	});

	// 获取用户打卡信息
	function render() {
		axios.get("/api/user/clockInInfo").then((res) => {
			// console.log("打卡信息>>>", res);
			dom.get(".clockIn .days").textContent = res.data.data.clockCount;
			if (res.data.data.isClockIn) {
				dom.get(".clockIn .clockBtn").disabled = true;
				dom.get(".clockIn .clockBtn").style.background = "#fff";
				dom.get(".clockIn .clockBtn").style.color = "#666";
				dom.get(".clockIn .clockBtn").textContent = "已打卡";
			}
		});
	}

	render();
	// 获取打卡

	dom.get(".clockIn .clockBtn").addEventListener("click", function () {
		axios.post("/api/user/clockIn").then((res) => {
			if (res.data.errno === 0) {
				render();
			}
		});
	});
});
